package com.todod.base.module.base.controller.admin.rthistory;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.todod.base.framework.common.pojo.PageParam;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.CommonResult;
import com.todod.base.framework.common.util.object.BeanUtils;
import static com.todod.base.framework.common.pojo.CommonResult.success;

import com.todod.base.framework.excel.core.util.ExcelUtils;

import com.todod.base.framework.apilog.core.annotation.ApiAccessLog;
import static com.todod.base.framework.apilog.core.enums.OperateTypeEnum.*;

import com.todod.base.module.base.controller.admin.rthistory.vo.*;
import com.todod.base.module.base.dal.dataobject.rthistory.RtHistoryDO;
import com.todod.base.module.base.service.rthistory.RtHistoryService;

@Tag(name = "管理后台 - 历史数据管理")
@RestController
@RequestMapping("/base/rt-history")
@Validated
public class RtHistoryController {

    @Resource
    private RtHistoryService rtHistoryService;

    @PostMapping("/create")
    @Operation(summary = "创建历史数据管理")
    public CommonResult<Long> createRtHistory(@Valid @RequestBody RtHistorySaveReqVO createReqVO) {
        return success(rtHistoryService.createRtHistory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新历史数据管理")
    public CommonResult<Boolean> updateRtHistory(@Valid @RequestBody RtHistorySaveReqVO updateReqVO) {
        rtHistoryService.updateRtHistory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除历史数据管理")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteRtHistory(@RequestParam("id") Long id) {
        rtHistoryService.deleteRtHistory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得历史数据管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<RtHistoryRespVO> getRtHistory(@RequestParam("id") Long id) {
        RtHistoryDO rtHistory = rtHistoryService.getRtHistory(id);
        return success(BeanUtils.toBean(rtHistory, RtHistoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得历史数据管理分页")
    public CommonResult<PageResult<RtHistoryRespVO>> getRtHistoryPage(@Valid RtHistoryPageReqVO pageReqVO) {
        PageResult<RtHistoryDO> pageResult = rtHistoryService.getRtHistoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RtHistoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出历史数据管理 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRtHistoryExcel(@Valid RtHistoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RtHistoryDO> list = rtHistoryService.getRtHistoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "历史数据管理.xls", "数据", RtHistoryRespVO.class,
                        BeanUtils.toBean(list, RtHistoryRespVO.class));
    }
    @GetMapping("/cleanall")
    @Operation(summary = "一键物理删除历史年度数据")
    public void cleanAll() {
         rtHistoryService.cleanAll();
    }



    @DeleteMapping("/delete-year")
    @Operation(summary = "根据年份物理删除数据表数据")
    public CommonResult<Boolean> deleteByYear(@RequestParam("year") String year) {
        rtHistoryService.deleteByYear(year);
        return success(true);
    }



}